package org.coco.mybatis.binding;

import com.coco.demo.Test;
import lombok.Data;
import org.coco.mybatis.annotation.Param;
import org.coco.util.ValidationUtil;

import java.lang.reflect.Method;
import java.lang.reflect.Parameter;
import java.util.ArrayList;
import java.util.List;

/**
 * 解析dao层方法参数的
 */
@Data
public class MethodSignature {

    private Class<?> clazz; //参数的类型
    private Object value; //参数的值
    private String name;  //参数的名称

    public MethodSignature(){}

    public MethodSignature(Class<?> clazz,Object value,String name) {
        this.clazz = clazz;
        this.value = value;
        this.name = name;
    }

    public static List<MethodSignature> resolveParamsType (Object[] args, Method method) {
        String paramName = null;
        Class<?> paramClass;
        Object paramValue;
        List<MethodSignature> methodSignatures = new ArrayList<>();
        if(ValidationUtil.isEmpty(args)) {
            return methodSignatures;
        }
        Parameter[] parameters = method.getParameters();
        int var1 = parameters.length;
        MethodSignature methodSignature = null;
        for(int i = 0; i < var1; i ++) {
            Parameter parameters1 = parameters[i];
            paramClass = parameters1.getType();
            paramValue = args[i];
            //查看该参数是否有@Param注解
            if(!parameters1.isAnnotationPresent(Param.class)) {
                paramName = parameters1.getName();
            }else {
                Param param = parameters1.getAnnotation(Param.class);
                paramName = param.value();
            }
            methodSignature = new MethodSignature(paramClass,paramValue,paramName);
            methodSignatures.add(methodSignature);
        }
        return methodSignatures;
    }
}
